const webpack = require('webpack')
const path = require('path')
const proxyConfig = require('./config/proxyConfig.js')
const urlConfig = require('./config/config.js')

console.log(process.env.NODE_ENV)
function resolve(dir) {
  return path.join(__dirname, dir)
}

module.exports = {
  publicPath: './',
  productionSourceMap: false,
  lintOnSave: false,
  configureWebpack: config => {
    // 本地联调打开，不然没有debugger断点、JS文件也会压缩成乱码
    config.devtool = 'source-map'
    if (process.env.NODE_ENV == 'production') {
      config.plugins.forEach(val => {
        if (val.__pluginName === 'html') {
          val.options.configUrl = './config.js?v=' + Date.now()
        }
      })
      config.optimization.minimizer[0].options.extractComments = true
      config.optimization.splitChunks.minSize = 5*1024*1024
      // config.optimization.splitChunks.maxSize = 6*1024*1024
    }

    config.plugins.push(
      new webpack.DefinePlugin({
        APP: {
          CONFIG: JSON.stringify(urlConfig[process.env.NODE_ENV])
        }
      })
    )

    return {
      resolve: {
        alias: {
          '@root': resolve('./'),
          '@': resolve('./src'),
          '@lang': resolve('./src/lang'),
          '@api': resolve('./src/api'),
          '@dist': resolve('./dist'),
          langCustom: resolve('./src/lang')
        }
      }
    }
  },
  pages: {
    index: {
      entry: 'src/main.js',
      template: 'public/index.html',
      filename: 'index.html',
      chunks:['chunk-vendors','chunk-common','index']
    },
    cover: {
      entry: 'src/main2.js',
      template: 'public/form.html',
      filename: 'form.html',
      chunks:['chunk-vendors','chunk-common','cover']
    }
  },
  devServer: {
    // host: 'share-web-sit.dadp-sit.crcloud.com',
    host: 'crinfo-sit.test.crcloud.com',
    // host: 'crinfo-uat.test.crcloud.com',
    https: false,
    port: 80,
    open: true,
    historyApiFallback: true,
    noInfo: true,
    disableHostCheck: true,
    proxy: proxyConfig
  },
  css: {
    loaderOptions: {
      // 向 CSS 相关的 loader 传递选项
      less: {
        modifyVars: {
          '@btn-height-base': '30px',
          '@primary-color': '#f89c34'
        },
        javascriptEnabled: true
      }
    }
  },
  chainWebpack : config => {
    config.plugins.delete("prefetch")

    config.when(process.env.NODE_ENV == 'production', config => {
      config.plugin('webpackOptimize')
      // .use(
      //   webpack.optimize.LimitChunkCountPlugin, 
      //   // 限制生成的代码块(chunks)的数量
      //   [{ maxChunks: 5 }]
      // )
      .use(
        webpack.optimize.MinChunkSizePlugin, 
        // 指定代码块的最小字节数
        [{ minChunkSize: 50000 }]
      )
    })
  },
}
